package com.yimicloud.common.freemarker;

import java.util.Iterator;
import java.util.Map;

/**
 * Created by Justin on 2015/9/7.
 */
public class StringTemplate {
    private String str;
    private Map params;

    public StringTemplate(String str, Map params) {
        this.str = str;
        this.params = params;
    }

    public String toString() {
        String result = this.str;

        String key;
        String strValue;
        for(Iterator it = this.params.entrySet().iterator(); it.hasNext(); result = StringHelper.replace(result, "${" + key + "}", strValue)) {
            Map.Entry entry = (Map.Entry)it.next();
            key = (String)entry.getKey();
            Object value = entry.getValue();
            strValue = value == null?"":value.toString();
            if(key.equals("package") && strValue.contains(".")) {
                strValue = strValue.replace(".", "/");
            }
        }

        return result;
    }
}
